<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="服务名称：">
            <el-input
              v-model="listQuery.specName"
              class="input-width"
              placeholder="输入名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="分类：">
            <el-cascader
              v-model="serType"
              :options="serTypeList"
            ></el-cascader>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAdd()"
      >新增服务</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column
          label="ID"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column
          label="服务名称"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.specName }}</template>
        </el-table-column>
        <el-table-column
          label="分类"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.serverSpecName }}</template>
        </el-table-column>
        <el-table-column
          label="更新时间"
          width="160"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.updateTime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="240"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleReview(scope.$index, scope.row)"
            >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { prodSpecList } from '@/api/servMs'
import { serverspecList, prodSpecDelete } from '@/api/deptInst'
import { formatDate } from '@/utils/date'

const listToTree = (list, tree, parentId) => {
  list.forEach((item) => {
    // 判断是否为父级菜单
    if (item.parentId === parentId) {
      const child = {
        ...item,
        value: item.id,
        label: item.serverSpecName,
        children: []
      }
      // if (child.parentId !== 1) child.entry = ''
      if (!child.component) child.entry = ''
      // 迭代 list， 找到当前菜单相符合的所有子菜单
      listToTree(list, child.children, item.id)
      // 删掉不存在 children 值的属性
      if (child.children.length <= 0) {
        delete child.children
      }
      // 加入到树中
      tree.push(child)
    }
  })
}
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  prodSpecName: '',
  serverSpecId: ''
}
export default {
  name: 'CourseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      operateType: null,
      prodSpecOptions: [],
      loading: false,
      deptOptions: [],
      serTypeList: [],
      serType: []
    }
  },
  created () {
    const data = {
      parentId: '',
      isNeedPic: true,
      serverSpecName: '',
      serverSpecLevel: ''
    }
    serverspecList(data).then((response) => {
      listToTree(response.data, this.serTypeList, 0)
    })

    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleResetSearch () {
      this.serType = []
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.$confirm('是否要删除该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        prodSpecDelete({ prodSpecIds: row.id }).then((response) => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    handleReview (index, row) {
      this.$router.push({ path: '/serv-ms/reviewServ', query: { id: row.id } })
    },
    handleAdd () {
      this.$router.push({ path: '/serv-ms/addServ' })
    },
    handleUpdate (index, row) {
      this.$router.push({
        path: '/serv-ms/addServ',
        query: { id: row.id, isEdit: true }
      })
    },
    getList () {
      this.listLoading = true
      const params = {
        ...this.listQuery,
        serverSpecId: this.serType[this.serType.length - 1]
      }
      prodSpecList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
        console.log(response.data.list)
      })
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
